<template>
  <div>
    <el-dialog  
        v-bind="$attrs" 
        v-on="$listeners"
        :visible="Visible"
        width="40%"
        custom-class="dialog"
        :modal-append-to-body='false'
        append-to-body
        @close="close" 
        :title="formListTitle"
        :destroy-on-close='true'
        :close-on-click-modal='false'
        :close-on-press-escape='false'
        :center='true'
        >
        <el-form ref="inStockManageForm" :model="formListData" :rules="rules" label-width="100px" v-if="viewMark" id="printTest">
            <el-form-item label="入库编号：">
              <span>{{formListData.objectId}}</span>
            </el-form-item>
            <el-form-item label="配件名称：">
              <span>{{formListData.parts.name}}</span>
            </el-form-item>

             <el-form-item label="入库数量：" >
              <span>{{formListData.amount}}</span>
            </el-form-item>

            <el-form-item label="货架编号：" >
             <span>{{formListData.stallsNo}}</span>
            </el-form-item>

             <el-form-item label="存储仓库："  >
              <span>{{formListData.storage.name}}</span>
            </el-form-item>

             <el-form-item label="生产来源：" >
              <span>{{formListData.source.name}}</span>
            </el-form-item>

             <el-form-item label="入库人员：" >
              <span>{{formListData.operator.name}}</span>
            </el-form-item>

             <el-form-item label="是否合格：">
                  <span>{{formListData.status?'合格':'不合格'}}</span>
            </el-form-item>
        </el-form>

         <el-form ref="inStockManageForm" :model="formListData" :rules="rules" label-width="100px" v-else>
            <el-form-item label="配件名称"  prop="parts">
            <el-select v-model="formListData.parts" placeholder="请选择" value-key='objectId' :style="{width: '95%'}"
            
            >
              <el-option
                v-for="item in partsList"
                :key="item.value.objectId"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
           <el-form-item label="入库数量" prop="amount">
                <el-input v-model="formListData.amount" placeholder="请填写入库数量" clearable :style="{width: '95%'}"
                >
                </el-input>
            </el-form-item>
            
             <el-form-item label="存储仓库"  prop="storage">
            <el-select v-model="formListData.storage" placeholder="请选择" value-key='objectId' :style="{width: '95%'}"
            >
              <el-option
                v-for="item in storageList"
                :key="item.value.objectId"
                :label="item.label"
                :value="item.value">
                 <!-- <span style="float: left">{{ item.label }}</span>
                 <span style="float: right; color: #8492a6; font-size: 13px">存储容量:{{ item.value.upperlimit }}</span> -->
              </el-option>
            </el-select>
        </el-form-item>
         <el-form-item label="货架编号"  prop="stallsNo">
           <el-input v-model="formListData.stallsNo" autocomplete="off" :style="{width: '95%'}" placeholder="请输入"></el-input>
        </el-form-item>
          <el-form-item label="生产来源"  prop="source">
            <el-select v-model="formListData.source" placeholder="请选择" value-key='objectId' :style="{width: '95%'}"
            >
              <el-option
                v-for="item in sourceList"
                :key="item.value.objectId"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
            <el-form-item label="是否合格">
                <el-switch
                v-model="formListData.status"
                active-color="#67C23A"
                inactive-color="#C0C4CC">
                </el-switch>
            </el-form-item>
        </el-form>
       
        <div slot="footer" v-if="!viewMark">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="handelConfirm" :loading="isloading">确定</el-button>
        </div>
        <div slot="footer" v-else>
            <el-button @click="close">取消</el-button>
            <el-button type="primary" v-print="'#printTest'">打印</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    Visible:Boolean,
    editMark:Boolean,
    listData:Object,
    viewMark:Boolean,
    partsList:Array,
    storageList:Array,
    sourceList:Array,
  },
  components: {

  },
  data() {
      const blurText = async(rule, value, callback) => {  
        const boolean = new RegExp('^[1-9][0-9]*$').test(value)
        if (!boolean) {
          callback(new Error('输入必须为整数'))
        }else if(Number(value)<=0){
          callback(new Error('输入必须大于0'))
        }else if(Number(value)>99999){
          callback(new Error('输入必须小于等于99999'))
        }else{
          callback()
        }
   　}
    return {
      title:'',
      inheritAttrs: false,
      isloading:false,
      formData: {
        status:true
      },
      rules: {
        parts:[
          { required:true,trigger:'change',message:'请选择'}
        ],
        storage:[
          { required:true,trigger:'change',message:'请选择'}
        ],
        source:[
          { required:true,trigger:'change',message:'请选择'}
        ],
        stallsNo:[
            { required:true,trigger:'change',message:'请输入货架编号'}
        ],
        amount: [{
          required: true,
          message: '',
          trigger: 'blur',
          validator: blurText
        }],
      },
    
    };
  },
  computed:{
      formListData(){
          if(this.editMark){
              this.formData = this.listData
              return this.formData
          }
          return this.formData
      },
      formListTitle(){
          if(this.editMark){
              if(this.viewMark) return this.title = '入库单'
              return this.title = '编辑入库单'
          }
          return this.title = '填写入库单'
      }
  },
  created() {
    
  },
  mounted() {
      console.log(this.partsList);
  },
  beforeDestroy() {
    this.$off()
  },
  methods: {
   
    close() {
      this.$emit('close')
    },
    handelConfirm() {
      this.$refs['inStockManageForm'].validate(valid => {
        if (valid &&!this.isloading){
            console.log(this.formData.parts);
          if(Number(this.formData.amount)<this.formData.parts.lowamount){
               this.$utils.message.warning(this,'当前入库数量少于配件入库的最小数量')
          }else if(Number(this.formData.amount)>this.formData.storage.upperlimit){
              this.$utils.message.warning(this,'当前仓库存储容量已达上限，无法入库')
          }else{
              this.isloading=true
              this.editMark?this.saveData(true):this.saveData(false)
          }
          
        }else{
            this.$utils.message.warning(this,'请填写完整信息')
            return false;
        }
        
      })
    },
    saveData(flag){
        const query = this.$Bmob.Query('Stock')
        flag?query.set('id', this.listData.objectId):null
        const pointer1 = this.$Bmob.Pointer('Parts')
        const poiID1 = pointer1.set(this.formData.parts.objectId)
        const pointer2 = this.$Bmob.Pointer('wareHouse')
        const poiID2 = pointer2.set(this.formData.storage.objectId)
        const pointer3 = this.$Bmob.Pointer('Farms')
        const poiID3 = pointer3.set(this.formData.source.objectId)
        const pointer4 = this.$Bmob.Pointer('_User')
        const poiID4 = pointer4.set(this.UserId)
        query.set('parts',poiID1)
        query.set('storage',poiID2)
        query.set('source',poiID3)
        query.set('amount',Number(this.formData.amount))
        query.set('status',this.formData.status)
        query.set('stallsNo',this.formData.stallsNo)
        flag?null:query.set('operator',poiID4)
        query.set('edit_operator',poiID4)
        query.save().then(async res=>{
            await this.updateStorageLimitSpace(this.formData.storage.objectId,Number(this.formData.amount))
            this.$emit('save')
            this.isloading=false
            this.close()
            this.$utils.message.success(this,'保存成功')
        }).catch(err=>{
            this.isloading=false
            this.$utils.message.error(this,'保存失败')
        })
    },

    updateStorageLimitSpace(id,amount){
        const query = this.$Bmob.Query('wareHouse')
        return query.get(id).then(res => {
            res.increment('upperlimit',-amount)
            res.save()
        }).catch(err=>{
        })
    }
  },
};
</script>

<style scoped lang="scss">

/deep/.el-dialog {
      border-radius: 15px;
      z-index: 5;
      min-width: 620px;  
      margin-top: 5vh !important;
}

</style>



